<?php
require_once '../include/config.php';
require_once '../include/utils.class.php';
require_once '../include/orm.class.php';
$orm = new ORM();

$enrl_id = Utils::get_param('id', 0);

$opts = new stdClass();
$opts->cd = 't.enrolled_intake_id = :v1';
$opts->cdv = array(':v1' => $enrl_id);
$opts->sel = 't2.title__ AS title__, t.*, t1.times AS __times, t1.room_id AS __room_id, t1.room_name AS __room_name, t1.teacher_id AS __teacher_id, t1.teacher_name AS __teacher_name';
$opts->jt = 'LEFT JOIN pb_colg_class AS t1 ON t1.id = t.class_id LEFT JOIN pb_colg_student AS t2 ON t2.id = t.student_id';
$data = $orm->get('pb_colg_enrolled_class', NULL, NULL, NULL, NULL, $opts)->d;

$orm->close();
?>
<!DOCTYPE html>
<html>
  <head>
    <title>Time Table</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script type="text/javascript" src="../3rd_party/jquery/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="../3rd_party/jquery/jquery-ui-1.10.2.custom.min.js"></script>
    <link href='../3rd_party/fullcalendar/fullcalendar.css' rel='stylesheet' />
    <script src='../3rd_party/fullcalendar/fullcalendar.min.js'></script>
    <style>
      .fc-event-time { font-size: 14px !important; }
    </style>
  </head>
  <body>
    <div><h1><?php if (count($data[0]) > 0) echo $data[0]->title__; ?></h1></div>
    <div id="timetable" style="width: 950px;"></div>
    
    <script type="text/javascript">
      /*<![CDATA[*/
      var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
      
      $(function() {
        $('#timetable').empty();
        $('#timetable').fullCalendar({
          defaultView: 'agendaWeek',
          header: {
            left: '',
            center: '',
            right: ''
          },
          columnFormat: {
            week: 'ddd'
          },
          year: 2000,
          month: 0,
          date: 3,
          allDaySlot: false,
          height: 1600,
          contentHeight: 1600,
          weekends: true,
          theme: true,
          
          minTime: 9,
          maxTime: 23,
          
          timeFormat: 'HH:mm{-HH:mm}',
    
          editable: false,
          selectable: false,
    
          events: function(start, end, callback) {
            for(var idx in __data) {
              var itm = __data[idx];
              for (var idx2 in JSON.parse(itm.__times)) {
                var itm2 = JSON.parse(itm.__times)[idx2];
                if (itm.type__ == 'Language') {
                  var rslt = itm2.split('-');
                  for (var i = 3; i < 8; i++) {
                    var evt = {
                      start: '2000-01-0' + i + ' ' + rslt[0] + ':00',
                      end: '2000-01-0' + i + ' ' + rslt[1] + ':00',
                      allDay: 0,
                      title: itm.class_name + ' ' + ((itm.__room_id != 0) ? itm.__room_name : '') + ' ' + ((itm.__teacher_id != 0) ? itm.__teacher_name : ''),
                      description: ''
                    };
                    $("#timetable").fullCalendar('renderEvent', evt, true);
                  }
                } else {
                  var rslt = itm2.split('#');
                  var evt = {
                    start: rslt[0] + ':00',
                    end: rslt[1] + ':00',
                    allDay: 0,
                    title: itm.course_name + ' ' + itm.class_name + ' ' + ((itm.__room_id != 0) ? itm.__room_name : '') + ' ' + ((itm.__teacher_id != 0) ? itm.__teacher_name : ''),
                    description: ''
                  };
                  $("#timetable").fullCalendar('renderEvent', evt, true);
                }
              }
            }
          }
        });
        window.print();
      });
      /*]]>*/
    </script>
  </body>
</html>